<style lang="less">
    @import './login.less';
</style>
<style>
  html,body{
    width: 100%;
    height: 100%;
    background: #f0f0f0;
    overflow: hidden;
  }
  .main{
    height: 100%;
    width: 100%;
  }
</style>

<template>
    <div class="login" @keydown.enter="handleSubmit">
        <div class="login-con">
            <Card :bordered="false">
                <p slot="title">
                    <Icon type="log-in"></Icon>
                    欢迎登录
                </p>
                <div class="form-con">
                    <Form ref="loginForm" :model="form" :rules="rules">
                        <!--<FormItem prop="token">
                            <Input v-model="form.token">
                                    <span slot="prepend">
                                        <Icon type="person"></Icon>
                                    </span>
                            </Input>
                        </FormItem>-->
                        <FormItem prop="userName">
                            <Input v-model="form.userName">
                                <span slot="prepend">
                                    <Icon :size="16" type="person"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem prop="password">
                            <Input v-model="form.password">
                                <span slot="prepend">
                                    <Icon :size="14" type="locked"></Icon>
                                </span>
                            </Input>
                        </FormItem>
                        <FormItem>
                            <Button @click="handleSubmit" type="primary" long>登录</Button>
                        </FormItem>
                    </Form>
                    <p class="login-tip">输入任意用户名和密码即可</p>
                </div>
            </Card>
        </div>
    </div>
</template>

<script>
import Cookies from 'js-cookie'
import * as api from '../service'
import * as types from '../store/mutation-types'
import { mapMutations } from 'vuex'
import crypto from 'crypto'
export default {
    data () {
        return {
            form: {
                userName: '',
                password: ''
            },
            rules: {
                userName: [
                    { required: true, message: '账号不能为空', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '密码不能为空', trigger: 'blur' }
                ]
            }
        };
    },
    methods: {
        handleSubmit () {
            this.$refs.loginForm.validate((valid) => {
                if (valid) {
                  var a;
                  var md5 = crypto.createHash("md5");
                  md5.update(this.form.password);
                  this.form.password = md5.digest('hex');
                  this.$store.dispatch('LocalLogin', this.form).then(() => {
                    this.$store.commit('setAvator', 'https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=3448484253,3685836170&fm=27&gp=0.jpg');
                    this.$router.push({
                      name: 'home_index'
                    });
                  }).catch((e) => {
                    this.$Message.error({
                      content: e,
                      duration: 8
                    });
                  });
                }
            });
        }
    }
};
</script>

